package com.dataimport.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataimport.entity.targetentity.SchoolCalendar;
import com.dataimport.mapper.InsertSchoolCalendarMapper;
import com.dataimport.mapper.SelectSchoolCalendarMapper;
import com.dataimport.service.SchoolCalendarService;
import com.dataimport.utils.HasNullFieldUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 卑微小峰
 * @date 2022/9/27
 */
@Service
public class
SchoolCalendarServiceImpl extends ServiceImpl<InsertSchoolCalendarMapper, SchoolCalendar> implements SchoolCalendarService {

    private static long PAGE_SIZE = 1000;

    @Autowired
    private SelectSchoolCalendarMapper selectSchoolCalendarMapper;

    @Override
    public void dealWithSchoolCalendar() {
        long currentPage = 0L;
        while (true) {
            currentPage++;
            List<SchoolCalendar> schoolCalendarList = selectData(currentPage);
            if (ObjectUtils.isEmpty(schoolCalendarList)) {
                break;
            }
            insertData(schoolCalendarList);
            System.out.println("插入成功");
        }
    }

    public List<SchoolCalendar> selectData(long currentPage) {
        DynamicDataSourceContextHolder.push("mysql2");

        IPage<com.dataimport.entity.sourceentity.SchoolCalendar> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(PAGE_SIZE);
        IPage<com.dataimport.entity.sourceentity.SchoolCalendar> data = selectSchoolCalendarMapper.selectPage(page, null);
        DynamicDataSourceContextHolder.poll();

        List<com.dataimport.entity.sourceentity.SchoolCalendar> selectSchoolCalendarList = data.getRecords();
        List<SchoolCalendar> insertSchoolCalendarList = selectSchoolCalendarList.stream()
                .filter(schoolCalendar -> HasNullFieldUtil.inExistenceNull(schoolCalendar))
                .map(selectSchoolCalendar -> {
                    SchoolCalendar insertSchoolCalendar = new SchoolCalendar();
                    insertSchoolCalendar.setStartTime(DateUtil.parse(selectSchoolCalendar.getKsrq()));
                    insertSchoolCalendar.setEndTime(DateUtil.parse(selectSchoolCalendar.getJsrq()));
                    insertSchoolCalendar.setSchoolYear(selectSchoolCalendar.getSchoolYear() + "-" + (Integer.parseInt(selectSchoolCalendar.getSchoolYear()) + 1));
                    insertSchoolCalendar.setSemester(selectSchoolCalendar.getSemester());
                    return insertSchoolCalendar;
                })
                .collect(Collectors.toList());

        return insertSchoolCalendarList;

    }

    @Transactional(rollbackFor = Exception.class)
    public void insertData(List<SchoolCalendar> insertSchoolCalendarList) {
        try {
            DynamicDataSourceContextHolder.push("mysql1");
            this.saveBatch(insertSchoolCalendarList, insertSchoolCalendarList.size());
            DynamicDataSourceContextHolder.poll();
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }

}
